package com.lxxie.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by xielx on 20/9/11 15:10
 */
@RestController
public class TestController {
        private Logger log = LoggerFactory.getLogger(getClass());

        @RequestMapping({"/test", "/asy/callBack"})
        public Object test(HttpServletRequest request) throws IOException, InterruptedException {
                try {
                        Random random = new Random(Thread.currentThread().getName().hashCode() + System.currentTimeMillis());
                        String content = URLDecoder.decode(IOUtils.toString(request.getInputStream(), "utf-8"), "utf-8");
                        log.info("接收到请求：{}", content);
                        JSONObject json = JSON.parseObject(content);
                        TimeUnit.SECONDS.sleep(json.getInteger("seed"));
                        return String.format("请求：%s ==> 返回结果：%s", content, Thread.currentThread().getName() + random.nextLong());
                } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        StringWriter sw = new StringWriter();
                        PrintWriter pwd = new PrintWriter(sw, true);
                        e.printStackTrace(pwd);
                        return sw.getBuffer().toString();
                }
        }

        @RequestMapping({"/asis"})
        public Object asis(HttpServletRequest request) throws IOException, InterruptedException {
                Map<String,Object> result = new HashMap<>();
                try {
                        Random random = new Random(Thread.currentThread().getName().hashCode() + System.currentTimeMillis());
                        String content = URLDecoder.decode(IOUtils.toString(request.getInputStream(), "utf-8"), "utf-8");
                        log.info("接收到回调请求：{}", content);
                        JSONObject json = JSON.parseObject(content);
                        if ("1".equals(request.getParameter("error"))) {
                                throw new RuntimeException("混蛋，出错了呀！");
                        }
                        result.put("code","200");
                        result.put("message","回调成功!");
                } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        StringWriter sw = new StringWriter();
                        PrintWriter pwd = new PrintWriter(sw, true);
                        e.printStackTrace(pwd);
                        result.put("code","500");
                        result.put("message",sw.getBuffer().toString());
                }
                return result;
        }
}
